package com.softeem.ossmusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.ossmusic.comm.R;
import com.softeem.ossmusic.entity.User;
import com.softeem.ossmusic.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户列表",description = "根据提供的页码和数据条数实现分页查询")
    @Parameters({
            @Parameter(name = "page",description = "页码"),
            @Parameter(name = "limit",description = "数据条数")
    })
    @GetMapping("/user/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        //创建分页对象  limit ?,?
        IPage<User> ipage = new Page<>(page, limit);
        //执行查询(分页)
        List<User> list = userService.list(ipage);
        //返回结果
        return R.success().setData(list);
    }

    @Operation(summary = "用户登录",description = "根据用户名和密码实现登录")
    @Parameters({
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    @GetMapping("/user/login")
    public R login(String username,String password){
        return userService.login(username,password);
    }

    @Operation(summary = "用户注册",description = "根据用户名和密码实现注册")
    @PostMapping("/user/reg")
    public R register(@RequestBody User user){
        return userService.register(user);
    }
}
